package com.co.vote.web.controller.vote;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.co.common.utils.PageReq;
import com.co.server.constant.Constant;
import com.co.server.entity.vote.TClass;
import com.co.server.entity.vote.TVote;
import com.co.server.entity.vote.TVoteLabel;
import com.co.server.entity.vote.aide.SearchConstant;
import com.co.server.entity.vote.aide.SearchDto;
import com.co.server.entity.vote.aide.TClassQuery;
import com.co.server.entity.vote.aide.TVoteLabelQuery;
import com.co.server.entity.vote.aide.TVoteQuery;
import com.co.server.service.vote.TClassService;
import com.co.server.service.vote.TVoteLabelService;
import com.co.server.service.vote.TVoteService;

/**
 * 根据条件搜索（标签，分类）
 * @author ZYL
 * @date 2016年12月6日 下午5:01:04
 */
@Controller
@RequestMapping("/search")
public class SearchController {
	
	@Resource
	TClassService tClassService;
	@Resource
	TVoteService tVoteService;
	@Resource
	TVoteLabelService tVoteLabelService;
	
	@RequestMapping(value = "/result", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session,PageReq pageReq, SearchDto searchDto ) {
		
		Page<TVote> page =null;
		pageReq.setPageSize(10);
		//根据最新最热查询
		Integer tabIdInt = searchDto.getTabId();
		if(tabIdInt!=null){
			Byte tabId = tabIdInt.byteValue();
			if(tabId.equals(SearchConstant.SEARCH_TAB_NEW)){
				//最新
				pageReq.setSort(new Sort(Direction.DESC,"t.id"));
				page = tVoteService.queryPageList(new TVoteQuery(), pageReq);
				model.addAttribute("page", page);
				
			}
			if(tabId.equals(SearchConstant.SEARCH_TAB_HOT)){
				//最热	
				pageReq.setSort(new Sort(Direction.DESC,"t.vote_num"));
				page = tVoteService.queryPageList(new TVoteQuery(), pageReq);
				model.addAttribute("page", page);
			}
		}
		
		//根据分类查询
		Integer classId = searchDto.getClassId();
		if(classId!=null){
			TVoteQuery tVoteQuery = new TVoteQuery();
			tVoteQuery.setClassId(classId);
			pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
			page = tVoteService.queryPageList(tVoteQuery, pageReq);
			model.addAttribute("page", page);
		}
		
		//根据标签查询
		Integer labelId = searchDto.getLabelId();
		if(labelId!=null){
			TVoteLabelQuery tVoteLabelQuery = new TVoteLabelQuery();
			tVoteLabelQuery.setLabelId(labelId);
			List<TVoteLabel> voteLabelList = tVoteLabelService.queryList(tVoteLabelQuery);
			Set<Integer> voteIdSet = new HashSet<Integer>();
			for (TVoteLabel tVoteLabel : voteLabelList) {
				voteIdSet.add(tVoteLabel.getVoteId());
			}
			// 根据多个voteId获得voteList
			TVoteQuery tVoteQuery = new TVoteQuery();
			tVoteQuery.setVoteIds(voteIdSet);
			page = tVoteService.queryPageList(tVoteQuery,pageReq);
			model.addAttribute("page", page);
		}
		return "/search/result";
	}
}
